<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\admin\controller;

use think\facade\View;
use app\admin\BaseController;
use app\admin\model\Catalog;
use plugins\cms\admin\model\Banner as BannerModel;
use think\exception\ValidateException;
use plugins\cms\admin\validate\Banner as Validate;
/**
 * Banner管理
 */
class Banner extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $input  = input('post.');
            $search = ['catalog', 'keyword', 'status'];
            $append = [];
            $order  = [$input['prop'] => $input['order']];
            $count  = BannerModel::withSearch($search, $input)->count();
            $data   = BannerModel::withSearch($search, $input)->append($append)->order($order)->page($input['page'], $input['pageSize'])->select();
            return json(['status' => 'success','message' => '获取成功', 'data' => $data, 'count' => $count]);
        } else {
        	// 分类
            $catalog = Catalog::field('id,pid,title')->where('theme', theme())->where('status', 1)->order('sort', 'desc')->select();
            View::assign('catalog', $catalog);
            return View::fetch();
        }
    }

    /**
     * 保存新建的资源
     */
    public function save() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('save')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            BannerModel::create([
                'catalog_id'           => $input['catalog_id'],
                'title'                => $input['title'],
                'c_title'              => $input['c_title'],
                'show'                 => $input['show'],
                'description'          => $input['description'],
                'url'                  => $input['url'],
                'content'              => $input['content'],
                'cover'                => $input['cover'],
                'status'               => $input['status'],
                'sort'                 => $input['sort'],
            ]);
            return json(['status' => 'success', 'message' => '新增成功']);
        }
    }

    /**
     * 保存更新的资源
     */
    public function update() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('update')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $save = BannerModel::find($input['id']);
            $save->catalog_id          = $input['catalog_id'];
            $save->title               = $input['title'];
            $save->c_title             = $input['c_title'];
            $save->show                = $input['show'];
            $save->description         = $input['description'];
            $save->url                 = $input['url'];
            $save->content             = $input['content'];
            $save->cover               = $input['cover'];
            $save->status              = $input['status'];
            $save->sort                = $input['sort'];
            $save->save();
            return json(['status' => 'success', 'message' => '修改成功']);
        }
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            BannerModel::destroy(input('post.ids'));
            return json(['status' => 'success', 'message' => '删除成功']);
        }
    }
}